<?php

namespace app\models\search;

use app\models\CrmSystemDict;
use yii\base\Model;
use yii\data\ActiveDataProvider;

class CrmSystemDictSearch extends CrmSystemDict
{
    public function rules()
    {
        return [
            [['id','code_type','code_id','valid','flag'], 'integer'],
            [['code_type_name','display_value', 'code_table','code_field','memo'], 'safe'],
        ];
    }

    public function scenarios()
    {
        return Model::scenarios();
    }

    /**
     * @param array $传递的参数，数组
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = CrmSystemDict::find();
        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);
        //设置前端传来的需要查询的数据
        $this->load($params);
        if (!$this->validate()) {
            return $dataProvider;
        }
        $query->andFilterWhere([
        ]);
        $query
            ->andFilterWhere(['flag'=>$this->flag])
            ->andFilterWhere(['code_type'=>$this->code_type])
            ->andFilterWhere(['like','code_type_name',$this->code_type_name])
            ->andFilterWhere(['code_id'=>$this->code_id]);

        return $dataProvider;
    }
    /**
     * @param array $传递的参数，数组
     * @return ActiveDataProvider
     */
    public function specialSearch($params)
    {
        $query = CrmSystemDict::find();
        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);
        //设置前端传来的需要查询的数据
        $this->load($params);
        if (!$this->validate()) {
            return $dataProvider;
        }
        $query->andFilterWhere([
//            'id' => $this->id,
        ]);
        $query
            ->andFilterWhere(['flag'=>$this->flag])
            ->andFilterWhere(['code_type'=>$this->code_type])
            ->andFilterWhere(['code_id'=>$this->code_id]);

        if (is_array($params['CrmSystemDictSearch']['code_type_name'])){
            foreach ($params['CrmSystemDictSearch']['code_type_name'] as $k=>$v){
                $query->orFilterWhere(['code_type_name'=>$v]);
            }
        }
//        $query
//            ->andFilterWhere(['=', 'code_id', $params['search']['code_id']]);//接听电话

        return $dataProvider;
    }
}
